package com.fixme4trade.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;

@Entity
public class Seller implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	//@GeneratedValue
	@SequenceGenerator( name = "SELLER_ID", sequenceName = "SELLER_SEQ", allocationSize = 1 )  
	@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "SELLER_ID" )
	@Column(name="seller_id")
	private Long id;
	
	@OneToOne(cascade=CascadeType.ALL, optional=false, fetch=FetchType.EAGER, orphanRemoval=false)    
    @PrimaryKeyJoinColumn
    private User user;
	
	@OneToMany(mappedBy = "owner")	
	private List<Product> productList;
	
	@OneToMany(mappedBy = "owner")
	private List<ProductRule> productRules = new ArrayList<ProductRule>();
	
	public Seller() {
		
	}

	public long getId() {
		return id;
	}

	public void setId(final Long id) {
		this.id = id;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(final User user) {
		this.user = user;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(final List<Product> productList) {		
		this.productList = productList;
	}

	public List<ProductRule> getProductRules() {
		return productRules;
	}

	public void setProductRules(final List<ProductRule> productRules) {
		this.productRules = productRules;
	}
	
	public List<Sell> getSells(){
		List<Sell> sells = new ArrayList<Sell>();
		for (Product p : getProductList()) {
			sells.addAll(p.getSellList());
		}
		return sells;
	}

	public void addProduct(final Product product) {
		if (productList == null)
			productList = new ArrayList<Product>();
			
		productList.add(product);
	}
}